package com.hexb.smh.aop;

import com.hexb.core.utils.ObjectUtils;
import com.hexb.smh.entity.enums.ILogCreator;
import com.hexb.smh.entity.enums.Operators;
import com.hexb.smh.entity.view.ProductView;
import com.hexb.smh.service.IProductService;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author : hexb
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ToggleProductLogCreator implements ILogCreator, InitializingBean {

    @NonNull
    private final OperationLogFactory factory;

    @NonNull
    private final IProductService productService;

    @Override
    public String create(Object[] args) {
        Boolean activated = (Boolean) args[1];
        Integer id = (Integer) args[0];
        String s = activated ? "上架项目" : "下架项目";
        ProductView productView = productService.selectOneById(id);
        if (productView == null) {
            return String.format("%s:[%s],项目不存在", s, args[0]);
        }
        return String.format("%s:[%s]([%s])", s, productView.getName(), productView.getItemNo());
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        factory.register(Operators.toggleProduct, this);
    }
}
